package com.zhentao.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zhentao.common.Result;
import com.zhentao.mapper.DynamicMapper;
import com.zhentao.mapper.UsersMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * 数据面板控制器
 */
@RestController
@RequestMapping("/admin/dashboard")
@CrossOrigin(origins = "*")
public class DashboardController {
    
    @Autowired
    private UsersMapper usersMapper;
    
    @Autowired
    private DynamicMapper dynamicMapper;
    
    /**
     * 统计数据
     */
    @GetMapping("/stats")
    public Result<Map<String, Object>> stats() {
        try {
            Map<String, Object> stats = new HashMap<>();
            
            // 总用户数
            Long totalUsers = usersMapper.selectCount(null);
            stats.put("totalUsers", totalUsers);
            
            // VIP用户数
            QueryWrapper<com.zhentao.entity.Users> vipWrapper = new QueryWrapper<>();
            vipWrapper.gt("vip_level", 0);
            Long vipUsers = usersMapper.selectCount(vipWrapper);
            stats.put("vipUsers", vipUsers);
            
            // 动态总数
            Long totalDynamics = dynamicMapper.selectCount(null);
            stats.put("totalDynamics", totalDynamics);
            
            // 待审核动态
            QueryWrapper<com.zhentao.entity.Dynamic> auditWrapper = new QueryWrapper<>();
            auditWrapper.eq("audit_status", 0);
            Long pendingDynamics = dynamicMapper.selectCount(auditWrapper);
            stats.put("pendingDynamics", pendingDynamics);
            
            // 活动数量（暂时返回模拟数据，需要对接活动服务）
            stats.put("totalActivities", 56);
            
            // 成功案例（暂时返回模拟数据）
            stats.put("totalCases", 89);
            
            // 课程数量（暂时返回模拟数据）
            stats.put("totalCourses", 34);
            
            return Result.success(stats);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("统计失败：" + e.getMessage());
        }
    }
}

